package com.fyzs.mp3recorder.wav

import android.util.Log
import androidx.core.net.UriCompat
import com.fyzs.mp3recorder.decoders.IDecoder
import java.io.DataInputStream
import java.io.File
import java.lang.StringBuilder


/**
 *
 * */
class WavDecoder(val musicFile:File) :IDecoder{

    val TAG="WavDecoder"
    /**
     * 开始处理
     * RIFF
     * ID	0x00	4Byte	大端	'RIFF' (0x52494646)
     * Size	0x04	4Byte	小端	fileSize - 8
     * Type	0x08	4Byte	大端	'WAVE'(0x57415645)
     *
     * FMT
     * ID	0x00	4Byte	大端	'fmt ' (0x666D7420)
     * Size	0x04	4Byte	小端	16
     * AudioFormat	0x08	2Byte	小端	音频格式
     * NumChannels	0x0A	2Byte	小端	声道数
     * SampleRate	0x0C	4Byte	小端	采样率
     * ByteRate	0x10	4Byte	小端	每秒数据字节数
     * BlockAlign	0x14	2Byte	小端	数据块对齐
     * BitsPerSample	0x16	2Byte	小端	采样位数
     *
     * DATA
     * ID	0x00	4Byte	大端	'data' (0x64617461)
     * Size	0x04	4Byte	小端	N
     * Data	0x08	NByte	小端	音频数据
     * */
    val riffId = ByteArray(4)
    val riffSize = ByteArray(4)
    val riffType = ByteArray(4)

    val fmtId = ByteArray(4)
    val fmtSize = ByteArray(4)
    val audioFormat = ByteArray(2)
    val channelNum = ByteArray(2)
    val sampleRate = ByteArray(4)
    val byteRate = ByteArray(4)
    val blockAlign = ByteArray(2)
    val bitsPerSample = ByteArray(2)

    val dataId = ByteArray(4)
    val dataSize = ByteArray(4)


    init {
        readFileInfo(musicFile)
    }

    override fun readFileInfo(file: File) {
        val dataInputStream = DataInputStream(file.inputStream())
        //先读riff block
        //RIFF id  4 byte
        dataInputStream.read(riffId)
        dataInputStream.read(riffSize)
        dataInputStream.read(riffType)
        //读取fmt block
        dataInputStream.read(fmtId)
        dataInputStream.read(fmtSize)
        dataInputStream.read(audioFormat)
        dataInputStream.read(channelNum)
        dataInputStream.read(sampleRate)
        //读取data
        dataInputStream.read(dataId)
        dataInputStream.read(dataSize)

        dataInputStream.close()
    }

    override fun getFileName(): String {
        TODO("Not yet implemented")
    }

    override fun getTimeLong(): String {
        //获取播放时长  dataSize = sampleRate * time * channelNum


        val dataSizeInt = 0
    }

    override fun getSampleRate(): Int {
        TODO("Not yet implemented")
    }

    override fun getChannelNum(): Int {
        TODO("Not yet implemented")
    }

    override fun getSampleType(): Int {
        TODO("Not yet implemented")
    }

    override fun play() {
        TODO("Not yet implemented")
    }

    override fun stop() {
        TODO("Not yet implemented")
    }


}